<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>办公用品类型</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../../res/js/Tools.js" charset="utf-8"></script>
    <script src="../../../../../res/js/error.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../../res/layui/css/layui.css" media="all">
</head>
<body style="padding: 10px">
<form class="layui-form layui-form-pane" action="">
    <input type="hidden" id="pkOfficeSuppliesTypeId" name="pkOfficeSuppliesTypeId"/>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:200px">用品类型名字</label>
        <div class="layui-input-block" style="margin-left: 200px">
            <input type="text" name="officeSuppliesTypeName" required
                   id="officeSuppliesTypeName"
                   lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" style="width:200px">用品上级类型</label>
        <div class="layui-input-block" style="margin-left: 200px">
            <select name="officeSuppliesTypeSuperiorId" id="officeSuppliesTypeSuperiorId">
                <option value="">请选择类型</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" id="flows">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">立即提交</button>
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>
</form>

<script>
    var table2;
    let pkOfficeSuppliesTypeId=getQueryString("pkOfficeSuppliesTypeId");
    console.log(pkOfficeSuppliesTypeId);
    // 修改赋值
    layui.use(['form', 'table','laydate'], function () {
        var table = layui.table
            , layer = layui.layer
            , form = layui.form;

        //监听提交
        form.on('submit(demo1)', function (data) {
            console.log(data.field);
            let res = postAjax(getRootPath() + "SuppliesTypeAction/typeAddOrUpdate", data.field);
            console.log(res);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3001);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {

            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        $("#demo3").on("click", function () {
            //询问框
            layer.confirm('是否关闭该页面？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });



       if (pkOfficeSuppliesTypeId){
           loadType(pkOfficeSuppliesTypeId);
           findById(pkOfficeSuppliesTypeId);
       }else {
           loadType();
       }
        form.render();
    });
    function findById(pkOfficeSuppliesTypeId) {
        let url = getRootPath() + "/SuppliesTypeAction/typeFindById/" + pkOfficeSuppliesTypeId;
        let data = getAjaxById(url);
        $("#officeSuppliesTypeName").val(data.data.officeSuppliesTypeName);
        $("#pkOfficeSuppliesTypeId").val(data.data.pkOfficeSuppliesTypeId);
        $("#officeSuppliesTypeSuperiorId").val(data.data.officeSuppliesTypeSuperiorId);
        layui.form.render("select");
    }
    //上级类型
    function loadType(pkOfficeSuppliesTypeId) {
        console.log(pkOfficeSuppliesTypeId);
        let url = getRootPath() + "SuppliesTypeAction/selectType";
        let data;
        if (pkOfficeSuppliesTypeId){
           url=url+"?pkOfficeSuppliesTypeId="+pkOfficeSuppliesTypeId;
           data =getAjaxById(url);
            $.each(data.data, function (index, item) {
                $("#officeSuppliesTypeSuperiorId").append("<option value='" + item.pkOfficeSuppliesTypeId + "'>" + item.officeSuppliesTypeName + "</option>");
            });
        }else {
            console.log(url);
            data =getAjaxById(url);
            $.each(data.data, function (index, item) {
                console.log(item);
                $("#officeSuppliesTypeSuperiorId").append("<option value='" + item.pkOfficeSuppliesTypeId + "'>" + item.officeSuppliesTypeName + "</option>");
            });
        }
    }
</script>
</body>
</html>